package com.achuna33.Controllers;

import com.achuna33.UI.UIController;
import com.achuna33.Utils.Cache;
import javafx.application.Platform;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import static com.achuna33.Utils.Cache.WriteLogBase;

public class Controller {
    public static Boolean FileOrLogArea = false;
    public static void WriteLog(String text){
        if (!text.startsWith("\n[")){
            text = text.replace("\n","");
            text ="\n[*]" +text;
        }

        if (Cache.uiController.targetAddress.getText().startsWith("file:")){
            System.out.println(Thread.currentThread().getId());
            String id = String.valueOf(Thread.currentThread().getId()) ;
            String path = Cache.ThreadIdForLog.get(id);
            WriteLog2File(path,text);
        }else {
            //Cache.uiController.logTextArea.setStyle("-fx-highlight-fill: lightgray; -fx-highlight-text-fill: firebrick; -fx-font-size: 20px;");
            WriteLogBase(text);
        }
    }
    public static void WriteExpLog(String text){
        if (!text.startsWith("\n[")){
            text = text.replace("\n","");
            text ="\n[*]" +text;
        }

        Cache.uiController.PublicArea.appendText(text);
    }
    public static void WriteLog2File(String filePath,String Content) {
        FileWriter fw = null;
        try {
            //如果文件存在，则追加内容；如果文件不存在，则创建文件
            filePath = java.net.URLDecoder.decode(filePath,"UTF-8");
            if (System.getProperties().getProperty("os.name").toLowerCase().contains("mac os")){
                filePath = "/" + filePath;
            }
            File f=new File(filePath);
            if (!f.exists()){
                f.createNewFile();
            }
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(Content);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void WriteSuccessLog(String text){
        if (text.startsWith("\n[*]")||text.startsWith("\n[+]")){
            text = text.substring(4);
            text = "\n[+]"+ text;
            WriteLogBase(text);
        }else {
            text = "\n[+]"+ text;
            WriteLogBase(text);
        }
    }
    public static void WriteFailLog(String text){
        if (text.startsWith("\n[*]")){
            text = text.substring(4);
            text = "\n[-]"+ text;
            WriteLogBase(text);
        }else if (text.startsWith("[*]")){
            text = text.substring(3);
            text = "\n[-]"+ text;
            WriteLogBase(text);
        } else {
            text = "\n[-]"+ text;
            WriteLogBase(text);
        }
    }


}
